import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:flutterapp/Base/Util/ShakeManager.dart';
import 'package:flutterapp/Base/methodChannel/MethodChannelManager.dart';
import 'package:flutterapp/Base/redux/reduxConfig.dart';
import 'package:flutterapp/Base/router/RouterConfig.dart';
import 'package:flutterapp/Base/style/MyStyles.dart';
import 'package:flutterapp/InitConfig.dart';
import 'package:fluttertoast/fluttertoast.dart';

final ThemeData theme = ThemeData();

class Application extends StatefulWidget {
  @override
  _ApplicationState createState() => _ApplicationState();
}

class _ApplicationState extends State<Application> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
      if (ENV == PUBLISHING_ENVIRONMENT.PROD) {
        Future.delayed(Duration(seconds: 1)).then((value) {
          MethodChannelManager.getInstance().invoke(MethodTypes.CHECK_UPDATE);
        });
      } else {
        ShakeManager.getInstance().startListening();
        Fluttertoast.showToast(msg: "当前为测试环境", toastLength: Toast.LENGTH_LONG);
      }
    });
  }

  @override
  void dispose() {
    if (ENV != PUBLISHING_ENVIRONMENT.PROD) {
      ShakeManager.getInstance().stopListening();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return StoreProvider<AppState>(
      store: appStore,
      child: MaterialApp(
        title: "flutterDemo",
        initialRoute: "/",
        routes: RouterMap,
        navigatorObservers: [MyNavigatorObserver.getInstance()],
        onUnknownRoute: (settings) {
          return PageRouteBuilder(pageBuilder: (context, __, ___) {
            return PageErr();
          });
        },
        theme: theme.copyWith(
          appBarTheme: AppBarTheme(
            centerTitle: true,
            color: MyColors.BACK_GROUND,
            titleTextStyle: MyTextStyle.GREY3_18,
            iconTheme: IconThemeData(color: MyColors.GREY333),
          ),
          colorScheme: theme.colorScheme.copyWith(
              secondary: MyColors.SECOND_COLOR,
              primary: MyColors.PRIMARY_COLOR),
        ),
          builder: EasyLoading.init(),
      ),
    );
  }
}
